<?php
// 导入基础类
require_once(dirname(__FILE__) . '/base.class.php');
class Brand extends Base
{
    function __construct()
    {
        // 调用父类的构造函数
        parent::__construct();
    }
    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }

    /*
        获取品牌列表
    */
    function lists()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];

        $where = ' where 1=1 ';
        // 获取查询参数
        if (isset($_GET['cate_id'])) {
            $cate_id = $_GET['cate_id'];
            $where .= " and cate_id={$cate_id}";
        }
        if (isset($_GET['no_page'])) {
            $no_page = $_GET['no_page'];
        }
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        if (!isset($no_page)) {
            $limit = " limit {$start},{$page_num}";
        }
        $sql = "select * from brands $where $limit";
        // $output['sql']=$sql;
        $list = $this->db->query($sql);
        if (!empty($list)) {
            // 将路径处理成绝对路径
            foreach ($list as $key => $item) {
                $list[$key]['brand_icon'] = path($item['brand_icon']);
                // 查询分类信息
                $cate = $this->db->get_one("select cate_name from categories where id={$item['cate_id']}");
                $list[$key]['cate_name'] = $cate['cate_name'];
            }
            // 获取总记录数
            $sql = "select count(id) as count from brands";
            $res = $this->db->get_one($sql);
            $output['data'] = ['list' => $list, 'total' => $res['count']];
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '获取品牌失败';
        }
        echo json_encode($output);
    }


    /*
        添加品牌
    */
    function add()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            $data = $_POST;
            $data['createtime'] = date('Y-m-d H:i:s');
            $query = arr2str($data);
            $sql = "insert into brands set $query";
            $res = $this->db->query($sql);
            if ($res) {
                // 将品牌id返回
                $output['cate_id'] = $res;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '操作失败';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用POST方式提交数据';
        }

        echo json_encode($output);
    }

    /*
        编辑品牌信息
    */
    function update()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        $id = $_GET['id'];
        if ($_POST) {
            $data = $_POST;
            $data['createtime'] = date('Y-m-d H:i:s');
            $query = arr2str($data);
            $sql = "update brands set $query where id={$id}";
            $res = $this->db->query($sql);
            $output['sql'] = $sql;
            if (!$res) {
                $output['errcode'] = 2;
                $output['msg'] = '操作失败';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用POST方式提交数据';
        }

        echo json_encode($output);
    }
    /*
        获取品牌
     */
    function detail()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $sql = "select * from brands where id={$id}";
            $res = $this->db->get_one($sql);
            if ($res) {
                $res['brand_icon'] = path($res['brand_icon']);
                $output['data'] = $res;
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }
    /*
     删除品牌
    */
    function del()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            if ($id > 16) {
                $sql = "delete from brands where id={$id}";
                $res = $this->db->query($sql);
                if (!$res) {
                    $output['errcode'] = 3;
                    $output['msg'] = '删除失败';
                }
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }
}
